#region Copyright (c) 2006-2009 Widgetsphere LLC, All Rights Reserved
//--------------------------------------------------------------------- *
//                          Widgetsphere  LLC                           *
//             Copyright (c) 2006-2009 All Rights reserved              *
//                                                                      *
//                                                                      *
//This file and its contents are protected by United States and         *
//International copyright laws.  Unauthorized reproduction and/or       *
//distribution of all or any portion of the code contained herein       *
//is strictly prohibited and will result in severe civil and criminal   *
//penalties.  Any violations of this copyright will be prosecuted       *
//to the fullest extent possible under law.                             *
//                                                                      *
//THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
//TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
//TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
//CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
//THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC   *
//                                                                      *
//UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
//PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
//SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT.    *
//                                                                      *
//THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
//CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE,      *
//INC.  THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO       *
//INSURE ITS CONFIDENTIALITY.                                           *
//                                                                      *
//THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
//PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
//EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
//THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
//SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                      *
//THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
//--------------------------------------------------------------------- *
#endregion
using System;
using System.Xml;
using System.Collections;
using System.ComponentModel;
using Widgetsphere.Generator.Common.GeneratorFramework;
using Widgetsphere.Generator.Common.Util;
using System.Collections.Generic;

namespace Widgetsphere.Generator.Models
{
	public class TableCompositeCollection : BaseModelCollection, IEnumerable<TableComposite>
	{
		#region Member Variables

		private Table _parent = null;
		private List<TableComposite> innerList = new List<TableComposite>();

		#endregion

		#region Constructor

		public TableCompositeCollection(IModelObject root, Table parent)
			: base(root)
		{
			_parent = parent;
		}

		#endregion

		#region IXMLable Members

		public override void XmlAppend(XmlNode node)
		{
			try
			{
				XmlDocument oDoc = node.OwnerDocument;

				XmlAttribute keyNode = oDoc.CreateAttribute("key");
				keyNode.Value = this.Key.ToString();
				node.Attributes.Append(keyNode);

				foreach (TableComposite composite in this)
				{
					XmlNode compositeNode = oDoc.CreateElement("composite");
					composite.XmlAppend(compositeNode);
					node.AppendChild(compositeNode);
				}

			}
			catch (Exception ex)
			{
				throw;
			}

		}

		public override void XmlLoad(XmlNode node)
		{
			try
			{
				_key = XmlHelper.GetAttributeValue(node, "key", _key);
				XmlNodeList compositeNodes = node.SelectNodes("composite");
				foreach (XmlNode compositeNode in compositeNodes)
				{
					TableComposite newComposite = new TableComposite(this.Root, this.Parent);
					newComposite.XmlLoad(compositeNode);
					this.Add(newComposite);
				}
				this.Dirty = false;
			}
			catch (Exception ex)
			{
				throw;
			}

		}

		#endregion

		#region Property Implementations

		public Table Parent
		{
			get { return _parent; }
		}

		#endregion

		#region IEnumerable Members

		public override IEnumerator GetEnumerator()
		{
			return innerList.GetEnumerator();
		}
		#endregion

		#region IDictionary Members

		internal TableComposite Add(TableComposite item)
		{
			innerList.Add(item);
			return item;
		}

		internal void Remove(TableComposite item)
		{
			innerList.Remove(item);			
		}

		public override void Clear()
		{
			innerList.Clear();
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		public TableComposite this[string name]
		{
			get
			{
				foreach (TableComposite element in this)
				{
					if (string.Compare(name, element.Name, true) == 0)
						return element;
				}
				return null;
			}
		}

		public override void AddRange(ICollection list)
		{
			foreach (TableComposite element in list)
			{
				innerList.Add(element);
			}
		}

		public TableComposite GetByKey(string key)
		{
			foreach (TableComposite element in this)
			{
				if (string.Compare(key, element.Key, true) == 0)
					return element;
			}
			return null;
		}

		public bool IsFixedSize
		{
			get { return false; }
		}

		public bool Contains(string name)
		{
			foreach (TableComposite composite in this)
			{
				if (string.Compare(composite.Name, name, true) == 0)
				{
					return true;
				}
			}
			return false;
		}

		public IEnumerable<Column> GetAllColumns()
		{
			return GetAllColumns(false);
		}

		public IEnumerable<Column> GetAllColumns(bool fullHierarchy)
		{
			List<TableComposite> TableCompositeList = new List<TableComposite>();
			if (fullHierarchy)
				TableCompositeList.AddRange(this);
			else
				TableCompositeList = this.Parent.GetTableCompositesFullHierarchy(true);				

			List<Column> retval = new List<Column>();
			foreach (TableComposite item in TableCompositeList)
			{
				foreach (Reference r in item.Columns)
				{
					retval.Add((Column)r.Object);
				}
			}
			return retval;
		}

		#endregion

		#region ICollection Members

		public override bool IsSynchronized
		{
			get { return false; }
		}

		public override int Count
		{
			get { return innerList.Count; }
		}

		public override void CopyTo(Array array, int index)
		{
			List<TableComposite> newList = new List<TableComposite>();
			foreach (TableComposite item in array)
			{
				newList.Add(item);
			}
			innerList.CopyTo(newList.ToArray(), index);
		}

		public override object SyncRoot
		{
			get { return null; }
		}

		#endregion

		#region IEnumerable<Table> Members

		IEnumerator<TableComposite> IEnumerable<TableComposite>.GetEnumerator()
		{
			return innerList.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		IEnumerator IEnumerable.GetEnumerator()
		{
			return innerList.GetEnumerator();
		}

		#endregion
	}
}